package com.wyy.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wyy.pojo.Comments;
import com.wyy.pojo.model.CommentQuery;
import com.wyy.response.CommonReturnType;
import com.wyy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-03 20:23
 */
@RestController
public class CommentAction {
    @Autowired
    private CommentService commentService;

    private static final String GET_PROVIDER_KEY = "gytcb-comment";

    /**
     * 用户进行评论
     *
     * @param comments
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod1")
    @RequestMapping(value = "/insertComment", method = RequestMethod.POST)
    public CommonReturnType tokenInsertComment(@RequestBody Comments comments) {
        Map<String, Object> map = commentService.insertComment(comments);
        return CommonReturnType.create(map);
    }

    public CommonReturnType blockHandlerMethod1(Comments comments, BlockException e) {
        return CommonReturnType.create("服务限流");
    }


    /**
     * 用户删除评论
     *
     * @param comments
     * @return
     */
    @RequestMapping(value = "/deleteComment", method = RequestMethod.POST)
    public CommonReturnType tokenDeleteComment(@RequestBody Comments comments) {
        Map<String, Object> map = commentService.deleteComment(comments);
        return CommonReturnType.create(map);
    }

    /**
     * 获取评论
     *
     * @param commentQuery
     * @return
     */
    @SentinelResource(value = GET_PROVIDER_KEY, blockHandler = "blockHandlerMethod2")
    @RequestMapping(value = "/getCommentBySomething", method = RequestMethod.POST)
    public CommonReturnType getCommentByArticleIdOrUserId(@RequestBody CommentQuery commentQuery) {
        Map<String, Object> map = commentService.getCommentByArticleIdOrUserId(commentQuery);
        return CommonReturnType.create(map);
    }

    public CommonReturnType blockHandlerMethod2(CommentQuery commentQuery, BlockException e) {
        return CommonReturnType.create("服务限流");
    }
}
